package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.annotation.RequiresRoles;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-02-19
 */
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
//@CrossOrigin(origins = "*", maxAge = 3600)
public class BrandController extends BaseController {

    @Autowired
    private IBrandService brandService;


    /**
     * 分页查询 ?name=米
     *
     * @param name
     * @return
     */
    @Operation(summary = "品牌分页查询")
    @GetMapping("/list")
    @RequiresPermissions("product:brand:list")
    public TableDataInfo list(@Parameter(description = "品牌名称") @RequestParam(value = "name", required = false) String name) {
        //1. 开始分页：从请求参数获取设置分页请求参数
        startPage();
        //2. 调用业务层查询分页数据
        List<Brand> list = brandService.findBrandListByPage(name);
        //3.封装分页结果
        return getDataTable(list);

    }

    /**
     * 根据id查询品牌信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取品牌详细信息")
    @GetMapping("/{id}")
    @RequiresPermissions("product:brand:query")
    public AjaxResult getBrandById(@PathVariable Long id) {
        Brand brand = brandService.getById(id);
        return AjaxResult.success(brand);
    }

    /**
     * 修改品牌信息
     *
     * @param brand
     * @return
     */
    @Operation(summary = "修改品牌")
    @PutMapping
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    public AjaxResult updateBrand(@Parameter(description = "品牌") @RequestBody Brand brand) {
        brand.setUpdateBy(SecurityUtils.getUsername());
        brandService.updateById(brand);
        return AjaxResult.success();
    }


    @Operation(summary = "新增品牌")
    @PostMapping
    @RequiresPermissions("product:brand:add")
    //@RequiresRoles("admin")
    @Log(title = "品牌新增", businessType = BusinessType.INSERT)
    public AjaxResult add(@Parameter(description = "品牌") @RequestBody Brand brand) {
        brand.setCreateBy(SecurityUtils.getUsername());
        brandService.save(brand);
        return AjaxResult.success();
    }


    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    @RequiresPermissions("product:brand:remove")
    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
    public AjaxResult remove(@Parameter(description = "品牌id集合") @PathVariable List<Long> ids) {
        brandService.removeBatchByIds(ids);
        return AjaxResult.success();
    }


    /**
     * 查询品牌列表
     * @return
     */
    @Operation(summary = "查询品牌列表")
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll(){
        List<Brand> list = brandService.list();
        return AjaxResult.success(list);
    }

}
